var service = new EbeimService();
$(function(){
	setInterval("getUpDownloadFaileds()",2500);
});
var $uploadFailed = 0;
var $downloadFailed = 1;
function showUpDownloadFaileds(flag){
	showDialog("upDownloadAgainWindow");
	if(flag == $uploadFailed){
		$("#upDownloadAgainWindow .dialogTipContentSpan").html("上传失败记录");
		$("#tryAgainBtn").html("重新上传")
		$("#tryAgainBtn").parent().attr("tryAgainType",$uploadFailed);
		service.getUpDownloadFaileds({
			type:$uploadFailed
		}, showUpDownloadFailedsCB);
	}else if(flag == $downloadFailed){
		$("#upDownloadAgainWindow .dialogTipContentSpan").html("下载失败记录");
		$("#tryAgainBtn").html("重新下载")
		$("#tryAgainBtn").parent().attr("tryAgainType",$downloadFailed);
		service.getUpDownloadFaileds({
			type:$downloadFailed
		}, showUpDownloadFailedsCB);
	}
}
function getUpDownloadFaileds(){
	var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
	if(tryType==undefined||tryType==""){
		tryType = 0;
	}
	service.getUpDownloadFaileds({
		type:tryType
	}, showUpDownloadFailedsCB);
}
var showUpDownloadFailedsCB = function(data){
	var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
	var uploadFailedContent = "";
	for(var i=0;i<data.beans.length;i++){
		var bean = data.beans[i];
		var status = bean.status;
		status = getStatusExp(tryType, status);
		uploadFailedContent = uploadFailedContent
		+"<tr>"
		+"	<td style='border-bottom:solid 1px #ddd;padding:10px 0'>"
		+		"<input type='hidden' class='failedClass' value='"+bean.id+"' />"
		+"		<nobr style='max-width:300px;display:inline-block;overflow:hidden;text-overflow:ellipsis;'>"
		+bean.root+bean.path+"\\"+bean.name
		+"		</nobr>"
		+"	</td>"
		+"	<td>"
		+status
		+"	</td>"
		+"</tr>";
	}
	$("#upDownloadAgainTB").html(uploadFailedContent);
}
function getStatusExp(tryType,status){
	if(tryType == $uploadFailed){
		if(status==0){
			status = "<font color=green>可重新上传</font>"
		}else if(status==1){
			status = "<font color=gray>队列中...</font>"
		}else if(status==-1){
			status = "<font color='#65A324'>备份中...</font>"
		}else if(status==2){
			status = "<font color=green>上传成功!</font>"
		}else if(status==3){
			status = "<font color=red>重新上传失败!</font>"
		}
	}else if(tryType == $downloadFailed){
		if(status==0){
			status = "<font color=green>可重新下载</font>"
		}else if(status==1){
			status = "<font color=gray>队列中...</font>"
		}else if(status==-1){
			status = "<font color='#65A324'>下载中...</font>"
		}else if(status==2){
			status = "<font color=green>下载成功!</font>"
		}else if(status==3){
			status = "<font color=red>重新下载失败!</font>"
		}
	}
	return status;
}
function tryAgain(){
	var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
	var ids = "";
	$(".failedClass").each(function(){
		var v = $(this).val();
		ids += v+",";
	});
	if(ids.length==0){
		plugin.msgtip({type:"e",msg:"暂无可传输数据..."});
	}else{
		ids = ids.substring(0,ids.length-1);
		service.upDownloadRetry({
			ids:ids,
			type:tryType
		},function(data){
			var type = "e"
			if(data.code=="200"){
				type = "o"
			}
			plugin.msgtip({type:type,msg:data.msg});
		});
	}
}

function deleteFaileds(){
	var ids = "";
	$(".failedClass").each(function(){
		var v = $(this).val();
		ids += v+",";
	});
	if(ids.length==0){
		plugin.msgtip({type:"e",msg:"没有可清空的数据了..."});
	}else{
		ids = ids.substring(0,ids.length-1);
		service.deleteUpDownloadFailedFiles({
			ids:ids
		}, function(data){
			plugin.msgtip({type:"i",msg:data.msg});
			var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
			if(tryType == $uploadFailed){
				showUpDownloadFaileds($uploadFailed);
			}else{
				showUpDownloadFaileds($downloadFailed);
			}
		});
	}
}